package com.war.session;

import com.ejb.persistence.Address;
import com.ejb.persistence.Author;
import com.ejb.persistence.Book;
import com.ejb.persistence.UserType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Ambroise
 */
@ManagedBean(name="userSession")
@SessionScoped
public class UserSession{
	
    private boolean isLogged;
    private Long userId;
    private UserType accessRight;
    private String firstName;
    private String lastName;
	private String login;
	private String email;
    
    private String password;
    private String passwordBis;
	
    private Address address;
    
    private Book book;
    private List<Book> books;
    private List<String> authorsId;

	/*
	 * The theme used by the user.
	 */
	private String theme = "default";

	/**
	 * All message displayed or need to displayed.
	 */
	private ArrayList<HashMap<String, Object>> messages;
	
	
	

	public ArrayList<HashMap<String, Object>> getMessages() {
		return messages;
	}

	public void setMessages(ArrayList<HashMap<String, Object>> messages) {
		this.messages = messages;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	
	public UserType getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(UserType accessRight) {
		this.accessRight = accessRight;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

	public boolean getIsLogged() {
		return isLogged;
	}

	public void setIsLogged(boolean isLogged) {
		this.isLogged = isLogged;
	}

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }
    
     public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordBis() {
        return passwordBis;
    }

    public void setPasswordBis(String passwordBis) {
        this.passwordBis = passwordBis;
    }

    public List<String> getAuthorsId() {
        if ((this.book != null && this.book.getAuthors() != null) && (this.authorsId == null || this.authorsId.isEmpty())){
            authorsId = new ArrayList<String>();
            for (Author a:book.getAuthors()){
                authorsId.add(a.getId().toString());
            }
        }
        return authorsId;
    }

    public void setAuthorsId(List<String> authorsId) {
        this.authorsId = authorsId;
    }

    
	/**
	 * Constructor
	 */
	public UserSession() {
		// Disconnected by default.
		this.isLogged = false;
		
		// Initialize array message.
		this.messages = new ArrayList<HashMap<String, Object>>();
	}
    
    /**
     * Check if the current user is an admin.
     * @return boolean - True/False
     */
    public boolean isAdmin(){
        return (this.accessRight == UserType.Libraire)? true : false ;
    }
    
    /**
     * Check if the current user is logged.
     * @return boolean - True/False
     */
    public boolean isLogged(){
        return (this.isLogged)? true : false ;
    }

}
